package com.yanqu.road.server.logic.thread.platform;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.thirdparty.PlatformMgr;
import com.yanqu.road.server.task.GameCommandTask;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.executor.GeneralTask;

public class ChangeNickNameTask extends GeneralTask {
    private GamePlayer player;
    private String nickName;

    public ChangeNickNameTask(GamePlayer player, String nickName) {
        super();
        this.player = player;
        this.nickName = nickName;
    }

    @Override
    public void doRun() {
        //修改昵称
        int ret = PlatformMgr.changeNickName(player, nickName, true, true);

        //响应
        if(0 == ret){//可以改名，则发一条协议给自己
            PlayerProto.ChangeNickNameReqMsg.Builder reqMsg = PlayerProto.ChangeNickNameReqMsg.newBuilder();
            reqMsg.setNickName(nickName);
            reqMsg.setIsServerSend(true);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessageByte(Protocol.S_CHANGE_NICK_NAME, player.getUserId(), reqMsg.build().toByteArray());
            GameCommandTask cmdWrapper = new GameCommandTask(null, pbMsg, player);
            if(null != player) {
                player.getCommandQueue().add(cmdWrapper);
            }
        }else {//改名失败，返回错误码
            PlayerProto.ChangeNickNameRespMsg.Builder respMsg = PlayerProto.ChangeNickNameRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setNickName(nickName);
            player.sendPacket(Protocol.U_CHANGE_NICK_NAME, respMsg);
        }
    }
}
